package com.developer.platform.controller.system;

import com.developer.platform.application.command.UserCmdManager;
import com.developer.platform.application.command.cmd.UserAdd;
import com.developer.platform.application.command.cmd.UserAndRoleUpdate;
import com.developer.platform.application.command.cmd.UserPasswordUpdate;
import com.developer.platform.application.command.cmd.UserUpdate;
import com.developer.platform.application.query.UserQueryManager;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.application.query.dto.UserAndRoleDto;
import com.developer.platform.application.query.dto.UserListDto;
import com.developer.platform.controller.BaseController;
import com.veeker.core.bean.Pageable;
import com.veeker.log.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Api(tags = "用户管理")
@Validated
@RequestMapping("/system/user")
@RestController
public class UserController extends BaseController {

    private final UserQueryManager userQueryManager;
    private final UserCmdManager userCommandManager;


    public UserController(UserQueryManager userQueryManager,
                          UserCmdManager userCommandManager) {
        this.userQueryManager = userQueryManager;
        this.userCommandManager = userCommandManager;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "账号", name = "username", required = false),
            @ApiImplicitParam(value = "名称", name = "name", required = false),
            @ApiImplicitParam(value = "手机号", name = "phone", required = false),
            @ApiImplicitParam(value = "页大小", name = "pageSize", required = true),
            @ApiImplicitParam(value = "当前页", name = "pageNumber", required = true),
    })
    @Log(businessType = "用户管理",discription = "查询用户列表")
    @ApiOperation(value = "查询用户列表")
    @GetMapping
    public ResponseEntity<ResponseModel<UserListDto>> page(@RequestParam(value = "username" , required = false) String username,
                                                           @RequestParam(value = "name" , required = false) String name ,
                                                           @RequestParam(value = "phone" , required = false) String phone ,
                                                           Pageable pageable){
        return ok(userQueryManager.page(username, name, phone, pageable));
    }

    @ApiOperation(value = "查询用户总数")
    @GetMapping("/count")
    public ResponseEntity<Integer> count(){
        return ok(userQueryManager.count());
    }

    @Log(businessType = "用户管理",discription = "新增用户信息")
    @ApiOperation(value = "新增用户信息")
    @PostMapping
    public ResponseEntity<?> save(@Valid @RequestBody UserAdd userAdd){
        userCommandManager.save(userAdd);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "用户管理",discription = "查询用户信息")
    @ApiOperation(value = "查询用户信息")
    @GetMapping("/{id}")
    public ResponseEntity<UserAndRoleDto> info(@PathVariable("id") long id){
        return ResponseEntity.ok(userQueryManager.info(id));
    }

    @Log(businessType = "用户管理",discription = "修改用户信息")
    @ApiOperation(value = "修改用户信息")
    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable("id") long id, @Valid @RequestBody UserAndRoleUpdate userAndRoleUpdate){
        userCommandManager.update(id,userAndRoleUpdate);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "用户管理",discription = "修改个人信息")
    @ApiOperation(value = "修改个人信息")
    @PutMapping("/info")
    public ResponseEntity<?> update(@Valid @RequestBody UserUpdate userUpdate){
        userCommandManager.update(userUpdate);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "用户管理",discription = "修改用户状态")
    @ApiOperation(value = "修改用户状态")
    @PutMapping("/status/{id}")
    public ResponseEntity<?> updateStatus(@PathVariable("id") long id){
        userCommandManager.updateStatus(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "用户管理",discription = "修改密码")
    @ApiOperation(value = "修改密码")
    @PutMapping("/password")
    public ResponseEntity<?> updatePassword(@Valid @RequestBody UserPasswordUpdate userPasswordUpdate){
        userCommandManager.updatePassword(userPasswordUpdate);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
